package com.errol.batchprocessor.payroll.advisorimpl.online;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.renjia.constant.Constant;
import com.renjia.dao.RenLiWuYouUserBankMapper;
import com.renjia.dao.SelfEmployedConfigMapper;
import com.renjia.dao.SelfEmployedRecordMapper;
import com.renjia.entity.RenLiWuYouUserBank;
import com.renjia.entity.SelfEmployedConfig;
import com.renjia.entity.SelfEmployedRecord;
import com.renjia.enums.SelfEmployedRegistryStatusEnum;
import com.renjia.service.generic.renliwuyou.basic.pojo.RenLiWuYouDataPushStatusEnum;
import com.renjia.service.module.employeeaddition.advisorimpl.payroll.payload.PayrollEmployeeBatchPayload;
import com.renjia.service.module.employeeaddition.advisorimpl.payroll.payload.PayrollEmployeePiecePayload;
import com.renjia.service.module.employeeaddition.advisorimpl.payroll.payload.PayrollEmployeeSinglePayload;
import com.renjia.service.module.payroll.advisor.verify.PayrollVerifyBatchStartAdvisor;
import com.renjia.service.module.payroll.advisor.verify.PayrollVerifySingleProcessAdvisor;
import com.renjia.service.module.payroll.advisorimpl.PayrollAdvisorTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 个体户校验的扩展点
 *
 * @author zhang xiao bin
 * @version v1.0
 * @date 2021/03/06
 * @since v5.0.6
 **/
@Service
public class SelfEmployeeAdvisorImpl
        implements PayrollVerifyBatchStartAdvisor, PayrollVerifySingleProcessAdvisor {

    private final SelfEmployedConfigMapper selfEmployedConfigMapper;
    private final SelfEmployedRecordMapper selfEmployedRecordMapper;
    private final RenLiWuYouUserBankMapper renLiWuYouUserBankMapper;

    public SelfEmployeeAdvisorImpl(SelfEmployedConfigMapper selfEmployedConfigMapper,
                                   SelfEmployedRecordMapper selfEmployedRecordMapper,
                                   RenLiWuYouUserBankMapper renLiWuYouUserBankMapper) {
        this.selfEmployedConfigMapper = selfEmployedConfigMapper;
        this.selfEmployedRecordMapper = selfEmployedRecordMapper;
        this.renLiWuYouUserBankMapper = renLiWuYouUserBankMapper;
    }

    @Override
    public boolean shallProcess(@NonNull PayrollEmployeeBatchPayload advisorPayload) {
        return PayrollAdvisorTypeEnum.DEFAULT == advisorPayload.getPayrollAdvisorTypeEnum();
    }

    @Override
    public int priority() {
        return 201;
    }

    @Override
    public boolean doPrepareOnBatchStartAtVerifyStage(@NonNull PayrollEmployeeBatchPayload batchPayload) {
        if (StringUtils.isNotBlank(batchPayload.getCoreChannelMerchant().getSelfEmployedConfigId())) {
            SelfEmployedConfig selfEmployedConfig = selfEmployedConfigMapper.selectById(batchPayload.getCoreChannelMerchant().getSelfEmployedConfigId());
            if (selfEmployedConfig != null) {
                if (Constant.GQ_AREA_CODE.equals(selfEmployedConfig.getAreaCode())) {
                    batchPayload.getPayLimitPayload().setNeedCheckGqc(true);
                }
            }
        }
        return true;
    }

    @Override
    public boolean doExecuteOnEmployeeInfoSavedAtVerifyStage(@NonNull PayrollEmployeeSinglePayload singlePayload, @NonNull PayrollEmployeePiecePayload piecePayload, @NonNull PayrollEmployeeBatchPayload batchPayload) {
        //4.8.8增加工商审核判断    5.2.0 个体户的判断移到人员信息处理成功后执行
        if (batchPayload.getPayLimitPayload().isNeedCheckGqc()) {
            Wrapper<SelfEmployedRecord> wrapper = Condition.wrapper();
            wrapper.eq("employee_id", singlePayload.getCoreEmployee().getId());
            wrapper.eq("self_employed_config_id", batchPayload.getCoreChannelMerchant().getSelfEmployedConfigId());
            List<SelfEmployedRecord> selfEmployedRecordList = selfEmployedRecordMapper.selectList(wrapper);
            if (selfEmployedRecordList != null && selfEmployedRecordList.size() > Constant.COMMON_ZERO) {
                if (!SelfEmployedRegistryStatusEnum.OFFICIAL_REVIEW_PASS.getCode().equals(selfEmployedRecordList.get(0).getRecordStatus())) {
                    singlePayload.getErrMsgList().add("尚未完成个体户注册");
                    return false;
                }
                Wrapper<RenLiWuYouUserBank> wuYouUserBankWrapper = Condition.wrapper();
                wuYouUserBankWrapper.eq("self_employed_record_id", selfEmployedRecordList.get(0).getId());
                wuYouUserBankWrapper.eq("self_employed_config_id", batchPayload.getCoreChannelMerchant().getSelfEmployedConfigId());
                wuYouUserBankWrapper.eq("bank_card", singlePayload.getSingleEntity().getRemark0());
                wuYouUserBankWrapper.eq("push_status", RenLiWuYouDataPushStatusEnum.SUCCESS.getCode());
                List<RenLiWuYouUserBank> wuYouUserBankList = renLiWuYouUserBankMapper.selectList(wuYouUserBankWrapper);
                if (wuYouUserBankList == null || wuYouUserBankList.size() == Constant.COMMON_ZERO) {
                    singlePayload.getErrMsgList().add("银行卡未在共青城绑定推送");
                    return false;
                }
            } else {
                singlePayload.getErrMsgList().add("尚未完成个体户注册");
                return false;
            }
        }
        return true;
    }

}
